# -*- coding: utf-8 -*-
from tool.gongju import *
from tool.zhibiao import ma_z

def 条件加仓网格策略_v1(cl_sj):
    """
    启动方式，为tick驱动
     一：开仓逻辑可自行设置胜率高的入场点进入，此策略下单点胜率越高越赚钱：

    # 初次开仓(当前策略出场下单逻辑
    if volume[-1] > volume[-2] and chicang == 0 and cl_sj.sjb['cczt'] == 0:  # 成交量大于5日的3倍，且为空仓
        if yunxukaicang and close[-1] < close[-2]:  # 满足初次开多
            pass

    else:  # 开空
    if yunxukaicang and close[-1] > close[-2]:  # 满足初次开空条件
        pass

   3.止盈：盈利大于等于8跳止盈

    二：加仓逻辑
   1.距离上次开仓价格亏损10跳、加仓2手 ，持仓均价盈利大于等于6跳止盈
   2.距离上次开仓价格亏损10跳、加仓4手 ，持仓均价盈利大于等于6跳止盈
   3.距离上次开仓价格亏损10跳、加仓8手 ，持仓均价盈利大于等于6跳止盈
   4.距离上次开仓价格亏损10跳、加仓16手，持仓均价盈利大于等于6跳止盈

   # 加仓数量可调整，在下面的程序段调整加仓列表
   jcsllb = [2, 4, 8, 16, 1]  # 每次加仓数量列表

    三：止损逻辑
    1.距离上次开仓点位亏损10跳、对手价止损平掉所有持仓。
    2.收盘前一分钟对手价平掉所有持仓并且不开新仓.
    注意事项：1.每次开新仓的同时就挂好加仓单和止盈单
            2.源码每行代码后汉字备注内容，（便于自己随时微调，不用总去麻烦你）
            3.实时价模型，只要满足条件一根K线可以发出多次信号 ，出信号一秒下单不复核

    参数可调设置： 第一次开仓止盈参数 1-50   8
                加仓开仓后止盈参数 1-50   6
                加仓距离上次开仓间隔点数 1-50  10

    四：晚上收盘和白天收盘都清仓

    """
    # 数据初始化，
    datetime = cl_sj.klines[0].datetime.values
    close = cl_sj.klines[0].close.values
    Open = cl_sj.klines[0].open.values
    volume = cl_sj.klines[0].volume.values
    canshu = [cl_sj.jiaoyiqidong, cl_sj.zhanghu, cl_sj.jiaoyishezhi, cl_sj.openingdata, datetime]  # 传递给交易函数用于交易
    chicang = cl_sj.openingdata['kaicangzhuangtai']
    chicangshuliang = cl_sj.openingdata['kaicangshuliang'] * chicang
    shijian = int(time_to_datetime(datetime[-1]).time().strftime("%H%M%S"))  # 当前时间
    zhbh = cl_sj.jiaoyishezhi[0][0]  # 开仓账户编号

    # 参数设置区
    ss = cl_sj.jiaoyishezhi[5][0]  # 初次开仓数量
    dyckczy = cl_sj.jiaoyishezhi[5][1]  # 第一次开仓止盈参数 1-50   8
    jchzy = cl_sj.jiaoyishezhi[5][2]  # 加仓开仓后止盈参数 1-50   6
    jcjj = cl_sj.jiaoyishezhi[5][3]  # 加仓距离上次开仓间隔点数 1-50  10
    zsj = cl_sj.jiaoyishezhi[5][4]  # 最后一次加仓后止损价
    wtpy = cl_sj.jiaoyishezhi[5][5]  # 委托偏移
    ypqcsj = cl_sj.jiaoyishezhi[5][6]  # 夜盘清仓时间
    volmazq = 2  # 成交量平均周期
    cjlbl = 1  # 成交量倍率
    cckcbz = 0  # 初次开仓标志，为1为有条件开仓
    jcsllb = [2, 4, 8, 16, 1]  # 每次加仓数量列表
    qingcangshijian = [145800, ypqcsj]  # 清仓时间
    jyhe = cl_sj.jiaoyishezhi[3][0]

    if 'cjl' not in cl_sj.sjb:  # 初始化时执行一次，用于初始化缓存字典
        chengjiaoliang = ma_z(volume[-60:-1], volmazq)
        cl_sj.sjb['cjl'] = round(chengjiaoliang[-1], 4)
        cl_sj.sjb['mbcw'] = ss  # 初始化目标仓位
        cl_sj.sjb['jcslsy'] = 0  # 加仓数量索引
        cl_sj.sjb['hydx'] = cl_sj.zhanghu[zhbh].get_zuixiaobiandong(jyhe)   # 最小变动单位
        cl_sj.sjb['jcgdjg'] = [0]  # 存放加仓挂单价
        cl_sj.sjb['zygdjg'] = [0]  # 存放加仓后止盈挂单价
        cl_sj.sjb['cczt'] = 0  # 持仓状态
        cl_sj.sjb['cckcsj'] = 8627352304  # 初次开仓时间
        cl_sj.sjb['xdid_zy'] = '8888'  # 止盈挂单id
        cl_sj.sjb['xdid_jc'] = '8888'  # 止盈挂单id
        cl_sj.sjb['xdid_ccxd'] = '8888'  # 初次挂单id
    dyckczy = dyckczy * cl_sj.sjb['hydx']  # 第一次开仓止盈参数 1-50   8
    jchzy = jchzy * cl_sj.sjb['hydx']  # 加仓开仓后止盈参数 1-50   6
    jcjj = jcjj * cl_sj.sjb['hydx']  # 加仓距离上次开仓间隔点数 1-50  10
    zsj = zsj * cl_sj.sjb['hydx']  # 最后一次加仓后止损价
    ccwtpy = 3*3*cl_sj.sjb['hydx']  # 出场委托偏移

    if cl_sj.jiaoyiqidong:  # 启动交易才执行下面程序
        # 允许交易时间控制
        yunxukaicang = (shijian > 90200 and shijian < 144800) or (shijian > 210200 and shijian < 235950) or \
                       (shijian > 0 and shijian < 23000)   # 允许开仓时间
        # 初次开仓
        if volume[-1] > volume[-2] and chicang == 0 and cl_sj.sjb['cczt'] == 0:  # 成交量大于5日的3倍，且为空仓
            if yunxukaicang and close[-1] < close[-2]:  # 满足初次开多条件

                kaicangjiage = cl_sj.tick['BidsJ'] + wtpy*cl_sj.sjb['hydx']  # 取排队价+偏移
                cl_sj.jiaoyishezhi[8][3] = kaicangjiage
                cl_sj.sjb['xdid_ccxd'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 开多
                print('初次开多=', cl_sj.jiaoyishezhi[3][0], kaicangjiage, ss, cl_sj.sjb['hydx'])
                cl_sj.sjb['cczt'] = 1
                dycgdjzy = kaicangjiage + dyckczy  # 第一次止盈挂单价
                gdjc1 = kaicangjiage - jcjj  # 第一次挂单加仓价
                gdjc2 = gdjc1 - jcjj  # 第二次挂单加仓价
                gdjc3 = gdjc2 - jcjj  # 第三次挂单加仓价
                gdjc4 = gdjc3 - jcjj  # 第三次挂单加仓价
                zhisun = gdjc4 - zsj  # 最后一次加仓后的止损价
                cl_sj.sjb['gdjg'] = [gdjc1, gdjc2, gdjc3, gdjc4, zhisun]  # 计算接下来加仓挂单价格
                jczyj1 = round(round(((kaicangjiage*ss+gdjc1*2)/3) / cl_sj.sjb['hydx']) * cl_sj.sjb['hydx'], 2) + jchzy
                jczyj2 = round(round(((kaicangjiage*ss+gdjc1*2+gdjc2*4)/7) / cl_sj.sjb['hydx']) * cl_sj.sjb['hydx'], 2) + jchzy
                jczyj3 = round(round(((kaicangjiage*ss + gdjc1*2 + gdjc2*4+gdjc3*8) / 15) / cl_sj.sjb['hydx']) * cl_sj.sjb['hydx'],2) + jchzy
                jczyj4 = round(round(((kaicangjiage*ss + gdjc1*2 + gdjc2*4 + gdjc3*8 + gdjc4*16) / 31) / cl_sj.sjb['hydx']) * cl_sj.sjb['hydx'],2) + jchzy
                cl_sj.sjb['zygdjg'] = [dycgdjzy, jczyj1, jczyj2, jczyj3, jczyj4]
                # Buy(ss, kaicangjiage, canshu)
                cl_sj.sjb['cckcsj'] = time.time()
                cckcbz = 1
            else:  # 开空
                if yunxukaicang and close[-1] > close[-2]:  # 满足初次开空条件

                    kaicangjiage = cl_sj.tick['AsksJ'] - wtpy * cl_sj.sjb['hydx']  # 取排队价+偏移
                    cl_sj.jiaoyishezhi[8][3] = kaicangjiage
                    cl_sj.sjb['xdid_ccxd'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 开空
                    print('初次开空=', cl_sj.jiaoyishezhi[3][0], kaicangjiage, ss, cl_sj.sjb['hydx'])
                    cl_sj.sjb['cczt'] = -1
                    dycgdjzy = kaicangjiage - dyckczy  # 第一次止盈挂单价
                    gdjc1 = kaicangjiage + jcjj  # 第一次挂单加仓价
                    gdjc2 = gdjc1 + jcjj  # 第二次挂单加仓价
                    gdjc3 = gdjc2 + jcjj  # 第三次挂单加仓价
                    gdjc4 = gdjc3 + jcjj  # 第三次挂单加仓价
                    zhisun = gdjc4 + zsj  # 最后一次加仓后的止损价
                    cl_sj.sjb['gdjg'] = [gdjc1, gdjc2, gdjc3, gdjc4, zhisun]  # 计算接下来加仓挂单价格
                    jczyj1 = round(round(((kaicangjiage*ss + gdjc1*2) / 3) / cl_sj.sjb['hydx']) * cl_sj.sjb['hydx'],2) - jchzy
                    jczyj2 = round(round(((kaicangjiage*ss + gdjc1*2 + gdjc2*4) / 7) / cl_sj.sjb['hydx']) * cl_sj.sjb['hydx'],2) - jchzy
                    jczyj3 = round(round(((kaicangjiage*ss + gdjc1*2 + gdjc2*4 + gdjc3*8) / 15) / cl_sj.sjb['hydx']) * cl_sj.sjb['hydx'],2) - jchzy
                    jczyj4 = round(round(((kaicangjiage*ss + gdjc1*2 + gdjc2*4 + gdjc3*8 + gdjc4*16) / 31) / cl_sj.sjb['hydx']) * cl_sj.sjb['hydx'], 2) - jchzy
                    cl_sj.sjb['zygdjg'] = [dycgdjzy, jczyj1, jczyj2, jczyj3, jczyj4]
                    # Buy(ss, kaicangjiage, canshu)
                    cl_sj.sjb['cckcsj'] = time.time()
                    cckcbz = 1

    # 账户仓位数据获取
    ccss = cl_sj.zhanghu[zhbh].get_chicang(jyhe)[0]
    # 账户持仓放入显示
    if ccss != chicangshuliang:
        MubiaoCangwei4_bxd(ccss, Open[-1], canshu)

    if cl_sj.jiaoyiqidong:  # 允许交易
        if cl_sj.sjb['cczt'] != 0:
            if cl_sj.sjb['cczt'] == 1 and ccss == cl_sj.sjb['mbcw'] and cl_sj.sjb['jcslsy'] <= 4:  # 挂多单
                if cl_sj.sjb['xdid_zy'] in cl_sj.zhanghu[zhbh].order_dict:  # 已进行止盈挂单
                    # if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_zy']]['kcdsl'] != 0:  # 可撤单数量不为0
                    cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_zy'])  # 撤上次止盈单
                    time.sleep(0.3)

                cl_sj.jiaoyishezhi[8][3] = cl_sj.sjb['zygdjg'][cl_sj.sjb['jcslsy']]  # 多止盈挂单价格
                cl_sj.sjb['xdid_zy'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ccss)  # 多止盈平仓挂单
                print('止盈挂单多=', cl_sj.jiaoyishezhi[3][0], cl_sj.sjb['zygdjg'][cl_sj.sjb['jcslsy']], ccss)

                cl_sj.sjb['mbcw'] = cl_sj.sjb['mbcw']+jcsllb[cl_sj.sjb['jcslsy']]  # 下一次的目标仓位

                # 初次仓开仓成交后进行挂单操作
                if cl_sj.sjb['jcslsy'] < 4:  # 挂多单加仓
                    cl_sj.jiaoyishezhi[8][3] = cl_sj.sjb['gdjg'][cl_sj.sjb['jcslsy']]
                    cl_sj.sjb['xdid_jc'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, jcsllb[cl_sj.sjb['jcslsy']])  # 加多
                    print('加仓挂单多=', cl_sj.jiaoyishezhi[3][0], cl_sj.sjb['gdjg'][cl_sj.sjb['jcslsy']], jcsllb[cl_sj.sjb['jcslsy']])
                cl_sj.sjb['jcslsy'] = cl_sj.sjb['jcslsy'] + 1  # 加仓数量索引加1
            else:
                ccss2 = abs(ccss)
                if cl_sj.sjb['cczt'] == -1 and ccss2 == cl_sj.sjb['mbcw'] and cl_sj.sjb['jcslsy'] <= 4:  # 挂空单
                    if cl_sj.sjb['xdid_zy'] in cl_sj.zhanghu[zhbh].order_dict:  # 已进行止盈挂单
                        # if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_zy']]['kcdsl'] != 0:  # 可撤单数量不为0
                        cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_zy'])  # 撤上次止盈单
                        time.sleep(0.3)

                    cl_sj.jiaoyishezhi[8][3] = cl_sj.sjb['zygdjg'][cl_sj.sjb['jcslsy']]  # 多止盈挂单价格
                    cl_sj.sjb['xdid_zy'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ccss2)  # 空止盈平仓挂单
                    print('止盈挂单空=', cl_sj.jiaoyishezhi[3][0], cl_sj.sjb['zygdjg'][cl_sj.sjb['jcslsy']], ccss2)

                    cl_sj.sjb['mbcw'] = cl_sj.sjb['mbcw'] + jcsllb[cl_sj.sjb['jcslsy']]  # 下一次的目标仓位

                    # 初次仓开仓成交后进行挂单操作
                    if cl_sj.sjb['jcslsy'] < 4:  # 挂多单加仓
                        cl_sj.jiaoyishezhi[8][3] = cl_sj.sjb['gdjg'][cl_sj.sjb['jcslsy']]
                        cl_sj.sjb['xdid_jc'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, jcsllb[cl_sj.sjb['jcslsy']])  # 加空
                        print('加仓挂单空=', cl_sj.jiaoyishezhi[3][0], cl_sj.sjb['gdjg'][cl_sj.sjb['jcslsy']],
                              jcsllb[cl_sj.sjb['jcslsy']])

                    cl_sj.sjb['jcslsy'] = cl_sj.sjb['jcslsy'] + 1  # 加仓数量索引加1

        # 止损
        if cl_sj.sjb['jcslsy'] > 4:  # 已经加仓4次，启动止损
            if ccss > 0 and cl_sj.sjb['cczt'] == 1:  # 多止损
                # print('多准备止损止损价=', cl_sj.jiaoyishezhi[3][0], cl_sj.sjb['gdjg'][-1], ccss)
                if cl_sj.tick['LastPrice'] <= cl_sj.sjb['gdjg'][-1]:
                    cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_zy'])
                    time.sleep(0.3)
                    cl_sj.jiaoyishezhi[8][3] =cl_sj.tick['LastPrice']-ccwtpy  # 止损价格
                    cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ccss)
                    # Sell(ccss, cl_sj.tick['LastPrice']-wtpy, canshu)
                    cl_sj.sjb['cczt'] = 0  # 开仓状态清0
                    cl_sj.sjb['jcslsy'] = 0  # 仓位索引清零
                    cl_sj.sjb['mbcw'] = ss  # 目标仓位调整为初始状态
                    print('多单止损=', cl_sj.jiaoyishezhi[3][0], cl_sj.tick['LastPrice']-ccwtpy, ccss)
            else:
                if ccss < 0 and cl_sj.sjb['cczt'] == -1:  # 空止损
                    # print('空准备止损止损价=', cl_sj.jiaoyishezhi[3][0], cl_sj.sjb['gdjg'][-1], ccss)
                    if cl_sj.tick['LastPrice'] >= cl_sj.sjb['gdjg'][-1]:
                        cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_zy'])
                        time.sleep(0.3)
                        cl_sj.jiaoyishezhi[8][3] = cl_sj.tick['LastPrice'] + ccwtpy  # 止损价格
                        cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, abs(ccss))
                        # BuyToCover(abs(ccss), cl_sj.tick['LowestPrice'] + wtpy, canshu)
                        cl_sj.sjb['cczt'] = 0  # 开仓状态清0
                        cl_sj.sjb['jcslsy'] = 0  # 仓位索引清零
                        cl_sj.sjb['mbcw'] = ss  # 目标仓位调整为初始状态
                        print('空单止损=', cl_sj.jiaoyishezhi[3][0], cl_sj.tick['LastPrice'] + ccwtpy, ccss)

        # 收盘清仓
        if shijian in qingcangshijian:  # 等于清仓时间
            if ccss > 0 and cl_sj.sjb['cczt'] == 1:  # 多止损
                cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_zy'])
                cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_jc'])
                time.sleep(0.3)
                cl_sj.jiaoyishezhi[8][3] = cl_sj.tick['LastPrice'] - ccwtpy  # 止损价格
                cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ccss)
                cl_sj.sjb['cczt'] = 0  # 开仓状态清0
                cl_sj.sjb['jcslsy'] = 0  # 仓位索引清零
                cl_sj.sjb['mbcw'] = ss  # 目标仓位调整为初始状态
                print('收盘清仓多=', cl_sj.jiaoyishezhi[3][0], cl_sj.tick['LastPrice'] - ccwtpy, ccss)
            else:
                if ccss < 0 and cl_sj.sjb['cczt'] == -1:  # 空止损
                    cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_zy'])
                    cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_jc'])
                    time.sleep(0.3)
                    cl_sj.jiaoyishezhi[8][3] = cl_sj.tick['LastPrice'] + ccwtpy  # 止损价格
                    cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, abs(ccss))
                    cl_sj.sjb['cczt'] = 0  # 开仓状态清0
                    cl_sj.sjb['jcslsy'] = 0  # 仓位索引清零
                    cl_sj.sjb['mbcw'] = ss  # 目标仓位调整为初始状态
                    print('收盘清仓空=', cl_sj.jiaoyishezhi[3][0], cl_sj.tick['LastPrice'] + ccwtpy, ccss)

        if cl_sj.sjb['jcslsy'] > 0:  # 已进行止盈挂单
            if cl_sj.sjb['xdid_zy'] in cl_sj.zhanghu[zhbh].order_dict:  # 已进行止盈挂单
                if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_zy']]['kcdsl'] == 0:  # 可撤单数量为0，代表止盈挂单已成交
                    cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_jc'])  # 撤到加仓挂单
                    cl_sj.sjb['cczt'] = 0  # 开仓状态清0
                    cl_sj.sjb['jcslsy'] = 0  # 仓位索引清零
                    cl_sj.sjb['mbcw'] = ss  # 目标仓位调整为初始状态

        if cl_sj.sjb['xdid_ccxd'] != '8888' and cl_sj.sjb['cczt'] != 0 and \
                cl_sj.sjb['xdid_ccxd'] in cl_sj.zhanghu[zhbh].order_dict:  # 初次下单60秒不成交撤单
            if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_ccxd']]['kcdsl'] != 0:
                if time.time() - cl_sj.sjb['cckcsj'] > 60:  # 已进行止盈挂单
                    cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_ccxd'])  # 撤初次挂单
                    time.sleep(0.2)
                    cl_sj.sjb['cczt'] = 0  # 开仓状态清0
                    cl_sj.sjb['jcslsy'] = 0  # 仓位索引清零
                    cl_sj.sjb['mbcw'] = ss  # 目标仓位调整为初始状态

    return cl_sj.sjb['jcslsy'], cl_sj.sjb['mbcw'], cl_sj.sjb['cczt'], cl_sj.tick['LastPrice']


def 根据底仓单挂网格交易多_1t(cl):
    """
    启动方式，为轮询
    需要开空策略需自己改造，这种网格是应对基本开底仓后，日常刷单用的。
    主观判断建好仓位后建好底仓后，价格反方向运动n1跳加仓1手，盈利n跳平一手，反复操作，加仓和止盈都为挂单,只多,单挂，实盘持仓不会越来越大
    1、过夜跳空开空多少就补多少
    2、底仓被止盈完后，就按底仓方向开一手，一直这样循环
    3、每次都加仓1手

    """
    # 数据提取区
    Open = cl.klines[0].open.values  # 开盘价np列表
    Datetime = cl.klines[0].datetime.values  # 日期时间np列表，为时间戳
    canshu = [cl.jiaoyiqidong, cl.zhanghu, cl.jiaoyishezhi, cl.openingdata, Datetime]  # 传递给交易函数用于交易
    riqishijian = time_to_datetime(Datetime[-1])
    shijian = int(riqishijian.time().strftime("%H%M%S"))  # 当前时间,格式为20210828
    chicang = cl.openingdata['kaicangzhuangtai']
    chicangshuliang = cl.openingdata['kaicangshuliang'] * chicang
    # kaicangjia = cl.openingdata['kaicangjia']  # 开仓价格

    # 参数设置区
    ss = cl.jiaoyishezhi[5][0]  # 开仓数量
    n1 = cl.jiaoyishezhi[5][1]  # 加仓止盈价格间隔，为跳
    夜盘收盘时间 = cl.jiaoyishezhi[5][2]
    清零数据库 = cl.jiaoyishezhi[5][3]  # 为0无用，为1时初始化数据库
    # 多空控制 = cl.jiaoyishezhi[5][2]  # 0为多，1为空
    zhanghu = cl.zhanghu[cl.jiaoyishezhi[0][0]]

    # 指标计算区
    # 初始化，初始化策略，和更新时运行一次
    if 'zxbd' not in cl.sjb:  # 初始化时执行一次，用于初始化缓存字典
        if not shujuku.get('celue.'+cl.name) or 清零数据库 == 1:  # 数据库中没有此字段，初始化数据库
            cl.sjb['zxbd'] = zhanghu.get_zuixiaobiandong(cl.jiaoyishezhi[3][0])  # 交易合约最小变动单位
            cl.sjb['开仓id'] = []
            cl.sjb['开仓jg'] = []
            cl.sjb['平仓id'] = []
            cl.sjb['平仓jg'] = []

            cl.sjb['补单id'] = '8'
            cl.sjb['补单状态'] = 0
            cl.sjb['挂单状态'] = 0
            cl.sjb['挂开仓价格'] = 0
            cl.sjb['基准价'] = 0
            cl.sjb['挂平仓价格'] = 0
            cl.sjb['收盘价'] = 0
            cl.sjb['收盘状态'] = 0  # 0，为初始状态，1为收盘，2为开盘
            shujuku.set('celue.'+cl.name, str(cl.sjb))

        durushuju = shujuku.get('celue.'+cl.name)
        cl.sjb = ast.literal_eval(durushuju)

    n1 = n1 * cl.sjb['zxbd']
    ccss = (chazhaoheyue_duiyingchicang(zhanghu.df_cc, cl.jiaoyishezhi[3][0]))[0]
    # 账户持仓放入显示
    if ccss != chicangshuliang:
        MubiaoCangwei4_bxd(ccss, Open[-1], canshu)

    if cl.shangcishijian != Datetime[-1]:  # 收盘运行
        shujuku.set('celue.'+cl.name, str(cl.sjb))  # 持久化全局变量

    # zuixinjia = cl.tick['LastPrice']  # 最新价
    zuixinjia = (DuquRedis_HQ(cl.jiaoyishezhi[3][0]))['LastPrice']
    if cl.jiaoyiqidong:  # 更新k线时执行，一般用与计算指标
        # 交易逻辑执行区
        if cl.sjb['收盘状态'] == 1 and (shijian == 210000 or shijian == 90000):
            tk = zuixinjia - cl.sjb['收盘价']  #
            lsblzt = False
            if tk > 0:
                lsbl = int(ccss - tk / cl.sjb['zxbd'])  # 跳空高开应该开多少手
                if lsbl > 0:
                    cl.jiaoyishezhi[8][3] = zuixinjia - n1  # 下单价格
                    zhanghu.pc_orders(cl.jiaoyishezhi, 1, lsbl)  # 平多
                    lsblzt = True
                    print(time.strftime("%Y-%m-%d %H:%M:%S"), " 跳空低开补平多")
            else:
                lsbl = int(abs(tk / cl.sjb['zxbd']))
                if lsbl > 2:
                    cl.jiaoyishezhi[8][3] = zuixinjia+n1  # 下单价格
                    zhanghu.kc_orders(cl.jiaoyishezhi, 1, lsbl)  # 开多
                    lsblzt = True
                    print(time.strftime("%Y-%m-%d %H:%M:%S"), " 跳空低开补开多")

            if lsblzt:
                zhanghu.query_positions()  # 查询持仓
                time.sleep(0.3)
                ccss = (chazhaoheyue_duiyingchicang(zhanghu.df_cc, cl.jiaoyishezhi[3][0]))[0]
            cl.sjb['收盘状态'] = 2

        # 初次挂单,或补单挂单
        if ccss > 0:
            if cl.sjb['挂单状态'] == 0 or cl.sjb['收盘状态'] == 2:  # or cl.sjb['挂单状态'] == 1:
                cl.sjb['基准价'] = zuixinjia
                cl.sjb['平仓id'] = []
                cl.sjb['平仓jg'] = []
                lsbl = 10 if ccss >= 10 else ccss
                for i in range(lsbl):
                    cl.sjb['基准价'] = cl.sjb['基准价'] + n1
                    cl.jiaoyishezhi[8][3] = cl.sjb['基准价']  # 下单价格
                    cl.sjb['平仓id'].append(zhanghu.pc_orders(cl.jiaoyishezhi, 1, ss))  # 平多
                    cl.sjb['平仓jg'].append(cl.sjb['基准价'])
                    time.sleep(0.1)

                cl.sjb['基准价'] = zuixinjia
                cl.sjb['开仓id'] = []
                cl.sjb['开仓jg'] = []
                for i in range(10):
                    cl.sjb['基准价'] = cl.sjb['基准价']-n1
                    cl.jiaoyishezhi[8][3] = cl.sjb['基准价']  # 下单价格
                    cl.sjb['开仓id'].append(zhanghu.kc_orders(cl.jiaoyishezhi, 1, ss))  # 开多
                    cl.sjb['开仓jg'].append(cl.sjb['基准价'])
                    time.sleep(0.1)

                cl.sjb['平仓id'] = cl.sjb['平仓id'][::-1]
                cl.sjb['开仓id'] = cl.sjb['开仓id'][::-1]
                cl.sjb['挂单状态'] = 1
                cl.sjb['补单状态'] = 0
                cl.sjb['收盘状态'] = 0  # 初始化开后盘状态
                print("初始挂单")

        else:
            if cl.sjb['补单状态'] == 0:  # 底仓没有了进行补仓
                cl.sjb['基准价'] = zuixinjia+n1
                cl.jiaoyishezhi[8][3] = cl.sjb['基准价']  # 下单价格
                cl.sjb['补单id'] = zhanghu.kc_orders(cl.jiaoyishezhi, 1, 5)  # 开多
                time.sleep(0.5)
                zhanghu.query_positions()  # 查询持仓
                cl.sjb['挂单状态'] = 0
                cl.sjb['补单状态'] = 1
                for i in cl.sjb['开仓id']:
                    zhanghu.cd_orders(i)  # 撤上次开仓挂单
                print(time.strftime("%Y-%m-%d %H:%M:%S"), " 补挂")

        # 有成交后挂单
        if cl.sjb['挂单状态'] == 1:
            if cl.sjb['开仓id'][-1] in zhanghu.order_dict:  # 已进行开仓挂单
                if zhanghu.order_dict[cl.sjb['开仓id'][-1]]['kcdsl'] == 0:  # 开仓已成交，补挂平仓
                    if len(cl.sjb['平仓id']) > 10:
                        zhanghu.cd_orders(cl.sjb['平仓id'][0])  # 撤上次止盈单
                        del cl.sjb['平仓id'][0]
                        del cl.sjb['平仓jg'][0]

                    if True:
                        cl.sjb['挂平仓价格'] = zhanghu.order_dict[cl.sjb['开仓id'][-1]]['kpjg'] + n1
                        cl.jiaoyishezhi[8][3] = cl.sjb['挂平仓价格']  # 下单价格
                        del cl.sjb['开仓id'][-1]
                        del cl.sjb['开仓jg'][-1]
                        cl.sjb['平仓id'].append(zhanghu.pc_orders(cl.jiaoyishezhi, 1,  ss))  # 平多
                        cl.sjb['平仓jg'].append(cl.sjb['挂平仓价格'])
                        if len(cl.sjb['开仓id']) < 10:
                            cl.sjb['挂开仓价格'] = min(cl.sjb['开仓jg']) - n1
                            cl.jiaoyishezhi[8][3] = cl.sjb['挂开仓价格']  # 下单价格
                            cl.sjb['开仓id'].insert(0, zhanghu.kc_orders(cl.jiaoyishezhi, 1, ss))  # 开多
                            cl.sjb['开仓jg'].insert(0, cl.sjb['挂开仓价格'])
                            print(time.strftime("%Y-%m-%d %H:%M:%S"), " 补挂开仓", cl.sjb['挂开仓价格'])

                        cl.sjb['挂单状态'] = 1
                        print(time.strftime("%Y-%m-%d %H:%M:%S"), " 开仓挂单已成交", cl.sjb['挂平仓价格'])

            if cl.sjb['平仓id'][-1] in zhanghu.order_dict:  # 已进行平仓挂单
                if zhanghu.order_dict[cl.sjb['平仓id'][-1]]['kcdsl'] == 0:  # 平仓挂单已成交
                    if len(cl.sjb['开仓id']) > 10:
                        zhanghu.cd_orders(cl.sjb['开仓id'][0])  # 开仓挂单太多进行测单
                        del cl.sjb['开仓id'][0]
                        del cl.sjb['开仓jg'][0]

                    if True:  # 底仓为1手，已成交的话，这里的ccss等于1，但实际已经为空仓了
                        cl.sjb['挂开仓价格'] = zhanghu.order_dict[cl.sjb['平仓id'][-1]]['kpjg'] - n1
                        cl.jiaoyishezhi[8][3] = cl.sjb['挂开仓价格']  # 下单价格
                        del cl.sjb['平仓id'][-1]
                        del cl.sjb['平仓jg'][-1]
                        cl.sjb['开仓id'].append(zhanghu.kc_orders(cl.jiaoyishezhi, 1,  ss))  # 开多
                        cl.sjb['开仓jg'].append(cl.sjb['挂开仓价格'])

                        if 0 < len(cl.sjb['平仓id']) < 10 and ccss > len(cl.sjb['平仓id']):
                            cl.sjb['挂平仓价格'] = max(cl.sjb['平仓jg']) + n1
                            cl.jiaoyishezhi[8][3] = cl.sjb['挂平仓价格']  # 下单价格
                            cl.sjb['平仓id'].insert(0, zhanghu.pc_orders(cl.jiaoyishezhi, 1, ss))  # 平多
                            cl.sjb['平仓jg'].insert(0, cl.sjb['挂平仓价格'])
                            print(time.strftime("%Y-%m-%d %H:%M:%S"), " 补挂平仓", cl.sjb['挂平仓价格'])

                        cl.sjb['挂单状态'] = 1
                        print(time.strftime("%Y-%m-%d %H:%M:%S"), " 平仓已成交", cl.sjb['挂开仓价格'])

        if ccss <= 0:
            zhanghu.query_positions()  # 查询持仓
            time.sleep(0.3)

        if shijian == 145950 or shijian == 夜盘收盘时间:
            cl.sjb['收盘价'] = zuixinjia
            if cl.sjb['收盘状态'] == 0:
                for i in cl.sjb['开仓id']:
                    zhanghu.cd_orders(i)  # 撤上次开仓挂单
                for i in cl.sjb['平仓id']:
                    zhanghu.cd_orders(i)  # 撤上次开仓挂单

                cl.sjb['收盘状态'] = 1
                print(time.strftime("%Y-%m-%d %H:%M:%S"), " 收盘工作,记录收盘价撤单等")

            shujuku.set('celue.' + cl.name, str(cl.sjb))

    return cl.sjb['挂单状态'], zuixinjia,  cl.sjb['挂开仓价格'], cl.sjb['挂平仓价格']


def 根据底仓单挂网格多_rn(cl):
    """
    启动方式，为轮询
    主观判断建好仓位后建好底仓后，价格反方向运动n1跳加仓1手，盈利n跳平一手，反复操作，加仓和止盈都为挂单,只多,单挂，实盘持仓不会越来越大
    1、过夜跳空开空多少就补多少
    2、底仓被止盈完后，就按底仓方向开一手，一直这样循环
    3、每次都加仓1手
    收盘清仓
    """
    # 数据提取区
    Open = cl.klines[0].open.values  # 开盘价np列表
    # High = cl.klines[0].high.values   # 最高价np列表
    # Low = cl.klines[0].low.values  # 最低价np列表
    # Close = cl.klines[0].close.values  # 收盘价np列表，Close[-1]这样为最新价
    Datetime = cl.klines[0].datetime.values  # 日期时间np列表，为时间戳
    canshu = [cl.jiaoyiqidong, cl.zhanghu, cl.jiaoyishezhi, cl.openingdata, Datetime]  # 传递给交易函数用于交易
    riqishijian = time_to_datetime(Datetime[-1])
    shijian = int(riqishijian.time().strftime("%H%M%S"))  # 当前时间,格式为20210828
    # riqi = int(riqishijian.date().strftime("%Y%m%d"))  # 当前日期，格式为161500
    chicang = cl.openingdata['kaicangzhuangtai']
    chicangshuliang = cl.openingdata['kaicangshuliang'] * chicang
    # kaicangjia = cl.openingdata['kaicangjia']  # 开仓价格

    # 参数设置区
    ss = cl.jiaoyishezhi[5][0]  # 开仓数量
    n1 = cl.jiaoyishezhi[5][1]  # 加仓止盈价格间隔，为跳
    夜盘收盘时间 = cl.jiaoyishezhi[5][2]
    清零数据库 = cl.jiaoyishezhi[5][3]  # 为0无用，为1时初始化数据库
    开仓挂单数 = cl.jiaoyishezhi[5][4]

    zhanghu = cl.zhanghu[cl.jiaoyishezhi[0][0]]
    heyue = cl.jiaoyishezhi[3][0]  # 交易的合约
    不交易时间 = [210000, 210100, 210200, 210300, 210400, 90000, 90100, 90200, 90300.90400, 145700, 145800, 145900, 225700,
             225800, 225900]
    # 指标计算区
    # 初始化，初始化策略，和更新时运行一次
    if 'zxbd' not in cl.sjb:  # 初始化时执行一次，用于初始化缓存字典
        if not shujuku.get('celue.'+cl.name) or 清零数据库 == 1:  # 数据库中没有此字段，初始化数据库
            cl.sjb['zxbd'] = zhanghu.get_zuixiaobiandong(cl.jiaoyishezhi[3][0])  # 交易合约最小变动单位
            cl.sjb['开仓id'] = []
            cl.sjb['开仓jg'] = []
            cl.sjb['平仓id'] = []
            cl.sjb['平仓jg'] = []

            cl.sjb['补单id'] = '8'
            cl.sjb['补单状态'] = 0
            cl.sjb['挂单状态'] = 0
            cl.sjb['挂开仓价格'] = 0
            cl.sjb['基准价'] = 0
            cl.sjb['挂平仓价格'] = 0
            cl.sjb['收盘价'] = 0
            cl.sjb['收盘状态'] = 0  # 0，为初始状态，1为收盘，2为开盘
            cl.sjb['挂单为空'] = False
            shujuku.set('celue.'+cl.name, str(cl.sjb))

        durushuju = shujuku.get('celue.'+cl.name)
        cl.sjb = ast.literal_eval(durushuju)

    n1 = n1 * cl.sjb['zxbd']
    ccss = (chazhaoheyue_duiyingchicang(zhanghu.df_cc, cl.jiaoyishezhi[3][0]))[0]
    # 账户持仓放入显示
    if ccss != chicangshuliang:
        MubiaoCangwei4_bxd(ccss, Open[-1], canshu)

    if cl.shangcishijian != Datetime[-1]:  # 收盘运行
        shujuku.set('celue.'+cl.name, str(cl.sjb))  # 持久化全局变量

    # zuixinjia = cl.tick['LastPrice']  # 最新价
    zuixinjia = (DuquRedis_HQ(cl.jiaoyishezhi[3][0]))['LastPrice']
    if cl.jiaoyiqidong:  # 更新k线时执行，一般用与计算指标
        # 初次挂单,或补单挂单
        if ccss > 0:
            if (cl.sjb['挂单状态'] == 0 or cl.sjb['挂单为空']) and shijian not in 不交易时间:
                cl.sjb['基准价'] = zuixinjia
                cl.sjb['平仓id'] = []
                cl.sjb['平仓jg'] = []
                lsbl = 开仓挂单数 if ccss >= 开仓挂单数 else ccss
                for i in range(lsbl):
                    cl.sjb['基准价'] = cl.sjb['基准价'] + n1
                    cl.jiaoyishezhi[8][3] = cl.sjb['基准价']  # 下单价格
                    cl.sjb['平仓id'].append(zhanghu.pc_orders(cl.jiaoyishezhi, 1, ss))  # 平多
                    cl.sjb['平仓jg'].append(cl.sjb['基准价'])
                    time.sleep(0.1)

                cl.sjb['基准价'] = zuixinjia
                cl.sjb['开仓id'] = []
                cl.sjb['开仓jg'] = []
                for i in range(开仓挂单数):
                    cl.sjb['基准价'] = cl.sjb['基准价']-n1
                    cl.jiaoyishezhi[8][3] = cl.sjb['基准价']  # 下单价格
                    cl.sjb['开仓id'].append(zhanghu.kc_orders(cl.jiaoyishezhi, 1, ss))  # 开多
                    cl.sjb['开仓jg'].append(cl.sjb['基准价'])
                    time.sleep(0.1)

                cl.sjb['平仓id'] = cl.sjb['平仓id'][::-1]
                cl.sjb['开仓id'] = cl.sjb['开仓id'][::-1]
                cl.sjb['挂单状态'] = 1
                cl.sjb['补单状态'] = 0
                cl.sjb['收盘状态'] = 0  # 初始化开后盘状态
                cl.sjb['挂单为空'] = False
                print(heyue, "初始挂单")

        else:
            if cl.sjb['补单状态'] == 0 and shijian not in 不交易时间:  # 底仓没有了进行补仓
                cl.sjb['基准价'] = zuixinjia+n1
                cl.jiaoyishezhi[8][3] = cl.sjb['基准价']  # 下单价格
                cl.sjb['补单id'] = zhanghu.kc_orders(cl.jiaoyishezhi, 1, 5)  # 开多
                time.sleep(0.5)
                zhanghu.query_positions()  # 查询持仓
                cl.sjb['挂单状态'] = 0
                cl.sjb['补单状态'] = 1
                for i in cl.sjb['开仓id']:
                    zhanghu.cd_orders(i)  # 撤上次开仓挂单
                print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 补挂")

        # 有成交后挂单
        if cl.sjb['挂单状态'] == 1 and shijian not in 不交易时间:
            if cl.sjb['开仓id']:
                if cl.sjb['开仓id'][-1] in zhanghu.order_dict:  # 已进行开仓挂单
                    if zhanghu.order_dict[cl.sjb['开仓id'][-1]]['kcdsl'] == 0:  # 开仓已成交，补挂平仓
                        if len(cl.sjb['平仓id']) > 开仓挂单数:
                            zhanghu.cd_orders(cl.sjb['平仓id'][0])  # 撤上次止盈单
                            del cl.sjb['平仓id'][0]
                            del cl.sjb['平仓jg'][0]

                        if True:
                            cl.sjb['挂平仓价格'] = zhanghu.order_dict[cl.sjb['开仓id'][-1]]['kpjg'] + n1
                            cl.jiaoyishezhi[8][3] = cl.sjb['挂平仓价格']  # 下单价格
                            del cl.sjb['开仓id'][-1]
                            del cl.sjb['开仓jg'][-1]
                            cl.sjb['平仓id'].append(zhanghu.pc_orders(cl.jiaoyishezhi, 1,  ss))  # 平多
                            cl.sjb['平仓jg'].append(cl.sjb['挂平仓价格'])
                            if len(cl.sjb['开仓id']) < 开仓挂单数:
                                cl.sjb['挂开仓价格'] = min(cl.sjb['开仓jg']) - n1
                                cl.jiaoyishezhi[8][3] = cl.sjb['挂开仓价格']  # 下单价格
                                cl.sjb['开仓id'].insert(0, zhanghu.kc_orders(cl.jiaoyishezhi, 1, ss))  # 开多
                                cl.sjb['开仓jg'].insert(0, cl.sjb['挂开仓价格'])
                                print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 补挂开仓", cl.sjb['挂开仓价格'])

                            cl.sjb['挂单状态'] = 1
                            print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 开仓挂单已成交", cl.sjb['挂平仓价格'])
            else:  # 开仓挂单列表为空
                if ccss > 0:
                    cl.sjb['挂单为空'] = True
                    for i in cl.sjb['平仓id']:
                        zhanghu.cd_orders(i)  # 撤上次开仓挂单

            if cl.sjb['平仓id']:
                if cl.sjb['平仓id'][-1] in zhanghu.order_dict:  # 已进行平仓挂单
                    if zhanghu.order_dict[cl.sjb['平仓id'][-1]]['kcdsl'] == 0:  # 平仓挂单已成交
                        if len(cl.sjb['开仓id']) > 开仓挂单数:
                            zhanghu.cd_orders(cl.sjb['开仓id'][0])  # 开仓挂单太多进行测单
                            del cl.sjb['开仓id'][0]
                            del cl.sjb['开仓jg'][0]

                        if True:  # 底仓为1手，已成交的话，这里的ccss等于1，但实际已经为空仓了
                            cl.sjb['挂开仓价格'] = zhanghu.order_dict[cl.sjb['平仓id'][-1]]['kpjg'] - n1
                            cl.jiaoyishezhi[8][3] = cl.sjb['挂开仓价格']  # 下单价格
                            del cl.sjb['平仓id'][-1]
                            del cl.sjb['平仓jg'][-1]
                            cl.sjb['开仓id'].append(zhanghu.kc_orders(cl.jiaoyishezhi, 1,  ss))  # 开多
                            cl.sjb['开仓jg'].append(cl.sjb['挂开仓价格'])

                            if 0 < len(cl.sjb['平仓id']) < 开仓挂单数 and ccss > len(cl.sjb['平仓id']):
                                cl.sjb['挂平仓价格'] = max(cl.sjb['平仓jg']) + n1
                                cl.jiaoyishezhi[8][3] = cl.sjb['挂平仓价格']  # 下单价格
                                cl.sjb['平仓id'].insert(0, zhanghu.pc_orders(cl.jiaoyishezhi, 1, ss))  # 平多
                                cl.sjb['平仓jg'].insert(0, cl.sjb['挂平仓价格'])
                                print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 补挂平仓", cl.sjb['挂平仓价格'])

                            cl.sjb['挂单状态'] = 1
                            print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 平仓已成交", cl.sjb['挂开仓价格'])
            else:  # 平仓挂单列表为空
                if ccss > 0:
                    cl.sjb['挂单为空'] = True
                    for i in cl.sjb['开仓id']:
                        zhanghu.cd_orders(i)  # 撤上次开仓挂单

        if ccss <= 0 and shijian not in 不交易时间:
            zhanghu.query_positions()  # 查询持仓
            time.sleep(0.3)

        if shijian == 145800 or shijian == 225800:
            cl.sjb['收盘价'] = zuixinjia
            if ccss > 0:
                for i in cl.sjb['开仓id']:
                    zhanghu.cd_orders(i)  # 撤上次开仓挂单
                cl.sjb['开仓id'] = []
                for i in cl.sjb['平仓id']:
                    zhanghu.cd_orders(i)  # 撤上次开仓挂单
                cl.sjb['平仓id'] = []
                time.sleep(1)
                # 清仓
                cl.jiaoyishezhi[8][3] = zuixinjia-n1
                cl.sjb['开仓id'].append(zhanghu.pc_orders(cl.jiaoyishezhi, 1, ccss))  # 平多
                cl.sjb['收盘状态'] = 1
                time.sleep(0.5)
                print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 收盘工作,记录收盘价撤单等")
            else:
                cl.sjb['开仓id'] = []
                cl.sjb['开仓jg'] = []
                cl.sjb['平仓id'] = []
                cl.sjb['平仓jg'] = []
                cl.sjb['补单id'] = '8'
                shujuku.set('celue.' + cl.name, str(cl.sjb))

    return cl.sjb['挂单状态'], zuixinjia,  cl.sjb['挂开仓价格'], cl.sjb['挂平仓价格']


def 根据底仓单挂网格空_rn(cl):
    """
    启动方式，为轮询
    主观判断建好仓位后建好底仓后，价格反方向运动n1跳加仓1手，盈利n跳平一手，反复操作，加仓和止盈都为挂单,只多,单挂，实盘持仓不会越来越大
    1、过夜跳空开空多少就补多少
    2、底仓被止盈完后，就按底仓方向开一手，一直这样循环
    3、每次都加仓1手
    收盘清仓
    """
    # 数据提取区
    Open = cl.klines[0].open.values  # 开盘价np列表
    # High = cl.klines[0].high.values   # 最高价np列表
    # Low = cl.klines[0].low.values  # 最低价np列表
    # Close = cl.klines[0].close.values  # 收盘价np列表，Close[-1]这样为最新价
    Datetime = cl.klines[0].datetime.values  # 日期时间np列表，为时间戳
    canshu = [cl.jiaoyiqidong, cl.zhanghu, cl.jiaoyishezhi, cl.openingdata, Datetime]  # 传递给交易函数用于交易
    riqishijian = time_to_datetime(Datetime[-1])
    shijian = int(riqishijian.time().strftime("%H%M%S"))  # 当前时间,格式为20210828
    # riqi = int(riqishijian.date().strftime("%Y%m%d"))  # 当前日期，格式为161500
    chicang = cl.openingdata['kaicangzhuangtai']
    chicangshuliang = cl.openingdata['kaicangshuliang'] * chicang
    # kaicangjia = cl.openingdata['kaicangjia']  # 开仓价格

    # 参数设置区
    ss = cl.jiaoyishezhi[5][0]  # 开仓数量
    n1 = cl.jiaoyishezhi[5][1]  # 加仓止盈价格间隔，为跳
    夜盘收盘时间 = cl.jiaoyishezhi[5][2]
    清零数据库 = cl.jiaoyishezhi[5][3]  # 为0无用，为1时初始化数据库
    开仓挂单数 = cl.jiaoyishezhi[5][4]
    zhanghu = cl.zhanghu[cl.jiaoyishezhi[0][0]]
    heyue = cl.jiaoyishezhi[3][0]  # 交易的合约
    不交易时间 = [210000, 210100, 210200, 210300, 210400, 90000, 90100, 90200, 90300.90400, 145700, 145800, 145900, 225700,
             225800, 225900]

    # 指标计算区
    # 初始化，初始化策略，和更新时运行一次
    if 'zxbd' not in cl.sjb:  # 初始化时执行一次，用于初始化缓存字典
        if not shujuku.get('celue.'+cl.name) or 清零数据库 == 1:  # 数据库中没有此字段，初始化数据库
            cl.sjb['zxbd'] = zhanghu.get_zuixiaobiandong(cl.jiaoyishezhi[3][0])  # 交易合约最小变动单位
            cl.sjb['开仓id'] = []
            cl.sjb['开仓jg'] = []
            cl.sjb['平仓id'] = []
            cl.sjb['平仓jg'] = []

            cl.sjb['补单id'] = '8'
            cl.sjb['补单状态'] = 0
            cl.sjb['挂单状态'] = 0
            cl.sjb['挂开仓价格'] = 0
            cl.sjb['基准价'] = 0
            cl.sjb['挂平仓价格'] = 0
            cl.sjb['收盘价'] = 0
            cl.sjb['收盘状态'] = 0  # 0，为初始状态，1为收盘，2为开盘
            cl.sjb['挂单为空'] = False
            shujuku.set('celue.'+cl.name, str(cl.sjb))

        durushuju = shujuku.get('celue.'+cl.name)
        cl.sjb = ast.literal_eval(durushuju)

    n1 = n1 * cl.sjb['zxbd']
    ccss = (chazhaoheyue_duiyingchicang(zhanghu.df_cc, cl.jiaoyishezhi[3][0]))[0]
    # 账户持仓放入显示
    if ccss != chicangshuliang:
        MubiaoCangwei4_bxd(ccss, Open[-1], canshu)
    ccss = abs(ccss)
    if cl.shangcishijian != Datetime[-1]:  # 收盘运行
        shujuku.set('celue.'+cl.name, str(cl.sjb))  # 持久化全局变量

    # zuixinjia = cl.tick['LastPrice']  # 最新价
    zuixinjia = float((DuquRedis_HQ(cl.jiaoyishezhi[3][0]))['LastPrice'])
    if cl.jiaoyiqidong:  # 更新k线时执行，一般用与计算指标
        # 初次挂单,或补单挂单
        if ccss > 0:
            if (cl.sjb['挂单状态'] == 0 or cl.sjb['挂单为空']) and shijian not in 不交易时间:
                cl.sjb['基准价'] = zuixinjia
                cl.sjb['平仓id'] = []
                cl.sjb['平仓jg'] = []
                lsbl = 开仓挂单数 if ccss >= 开仓挂单数 else ccss
                for i in range(lsbl):
                    cl.sjb['基准价'] = cl.sjb['基准价'] - n1
                    cl.jiaoyishezhi[8][3] = cl.sjb['基准价']  # 下单价格
                    cl.sjb['平仓id'].append(zhanghu.pc_orders(cl.jiaoyishezhi, -1, ss))  # 平空
                    cl.sjb['平仓jg'].append(cl.sjb['基准价'])
                    time.sleep(0.1)

                cl.sjb['基准价'] = zuixinjia
                cl.sjb['开仓id'] = []
                cl.sjb['开仓jg'] = []
                for i in range(开仓挂单数):
                    cl.sjb['基准价'] = cl.sjb['基准价']+n1
                    cl.jiaoyishezhi[8][3] = cl.sjb['基准价']  # 下单价格
                    cl.sjb['开仓id'].append(zhanghu.kc_orders(cl.jiaoyishezhi, -1, ss))  # 开空
                    cl.sjb['开仓jg'].append(cl.sjb['基准价'])
                    time.sleep(0.1)

                cl.sjb['平仓id'] = cl.sjb['平仓id'][::-1]
                cl.sjb['开仓id'] = cl.sjb['开仓id'][::-1]
                cl.sjb['挂单状态'] = 1
                cl.sjb['补单状态'] = 0
                cl.sjb['收盘状态'] = 0  # 初始化开后盘状态
                cl.sjb['挂单为空'] = False
                print(heyue, "初始挂单")

        else:
            if cl.sjb['补单状态'] == 0 and shijian not in 不交易时间:  # 底仓没有了进行补仓
                cl.sjb['基准价'] = zuixinjia-n1
                cl.jiaoyishezhi[8][3] = cl.sjb['基准价']  # 下单价格
                cl.sjb['补单id'] = zhanghu.kc_orders(cl.jiaoyishezhi, -1, 5)  # 开空
                time.sleep(0.5)
                zhanghu.query_positions()  # 查询持仓
                cl.sjb['挂单状态'] = 0
                cl.sjb['补单状态'] = 1
                for i in cl.sjb['开仓id']:
                    zhanghu.cd_orders(i)  # 撤上次开仓挂单
                print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 补挂")

        # 有成交后挂单
        if cl.sjb['挂单状态'] == 1 and shijian not in 不交易时间:
            if cl.sjb['开仓id']:
                if cl.sjb['开仓id'][-1] in zhanghu.order_dict:  # 已进行开仓挂单
                    if zhanghu.order_dict[cl.sjb['开仓id'][-1]]['kcdsl'] == 0:  # 开仓已成交，补挂平仓
                        if len(cl.sjb['平仓id']) > 开仓挂单数:
                            zhanghu.cd_orders(cl.sjb['平仓id'][0])  # 撤上次止盈单
                            del cl.sjb['平仓id'][0]
                            del cl.sjb['平仓jg'][0]

                        if True:
                            cl.sjb['挂平仓价格'] = zhanghu.order_dict[cl.sjb['开仓id'][-1]]['kpjg'] - n1
                            cl.jiaoyishezhi[8][3] = cl.sjb['挂平仓价格']  # 下单价格
                            del cl.sjb['开仓id'][-1]
                            del cl.sjb['开仓jg'][-1]
                            cl.sjb['平仓id'].append(zhanghu.pc_orders(cl.jiaoyishezhi, -1,  ss))  # 平空
                            cl.sjb['平仓jg'].append(cl.sjb['挂平仓价格'])
                            if len(cl.sjb['开仓id']) < 开仓挂单数:
                                cl.sjb['挂开仓价格'] = max(cl.sjb['开仓jg']) + n1
                                cl.jiaoyishezhi[8][3] = cl.sjb['挂开仓价格']  # 下单价格
                                cl.sjb['开仓id'].insert(0, zhanghu.kc_orders(cl.jiaoyishezhi, -1, ss))  # 开空
                                cl.sjb['开仓jg'].insert(0, cl.sjb['挂开仓价格'])
                                print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 补挂开仓", cl.sjb['挂开仓价格'])

                            cl.sjb['挂单状态'] = 1
                            print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 开仓挂单已成交", cl.sjb['挂平仓价格'])
                else:  # 开仓挂单列表为空
                    if ccss > 0:
                        cl.sjb['挂单为空'] = True
                        for i in cl.sjb['平仓id']:
                            zhanghu.cd_orders(i)  # 撤上次开仓挂单

            if cl.sjb['平仓id']:
                if cl.sjb['平仓id'][-1] in zhanghu.order_dict:  # 已进行平仓挂单
                    if zhanghu.order_dict[cl.sjb['平仓id'][-1]]['kcdsl'] == 0:  # 平仓挂单已成交
                        if len(cl.sjb['开仓id']) > 开仓挂单数:
                            zhanghu.cd_orders(cl.sjb['开仓id'][0])  # 开仓挂单太多进行测单
                            del cl.sjb['开仓id'][0]
                            del cl.sjb['开仓jg'][0]

                        if True:  # 底仓为1手，已成交的话，这里的ccss等于1，但实际已经为空仓了
                            cl.sjb['挂开仓价格'] = zhanghu.order_dict[cl.sjb['平仓id'][-1]]['kpjg'] + n1
                            cl.jiaoyishezhi[8][3] = cl.sjb['挂开仓价格']  # 下单价格
                            del cl.sjb['平仓id'][-1]
                            del cl.sjb['平仓jg'][-1]
                            cl.sjb['开仓id'].append(zhanghu.kc_orders(cl.jiaoyishezhi, -1,  ss))  # 开空
                            cl.sjb['开仓jg'].append(cl.sjb['挂开仓价格'])

                            if 0 < len(cl.sjb['平仓id']) < 开仓挂单数 and ccss > len(cl.sjb['平仓id']):
                                cl.sjb['挂平仓价格'] = min(cl.sjb['平仓jg']) - n1
                                cl.jiaoyishezhi[8][3] = cl.sjb['挂平仓价格']  # 下单价格
                                cl.sjb['平仓id'].insert(0, zhanghu.pc_orders(cl.jiaoyishezhi, -1, ss))  # 平空
                                cl.sjb['平仓jg'].insert(0, cl.sjb['挂平仓价格'])
                                print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 补挂平仓", cl.sjb['挂平仓价格'])

                            cl.sjb['挂单状态'] = 1
                            print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 平仓已成交", cl.sjb['挂开仓价格'])
            else:  # 平仓挂单列表为空
                if ccss > 0:
                    cl.sjb['挂单为空'] = True
                    for i in cl.sjb['开仓id']:
                        zhanghu.cd_orders(i)  # 撤上次开仓挂单

        if ccss <= 0 and shijian not in 不交易时间:
            zhanghu.query_positions()  # 查询持仓
            time.sleep(0.3)

        if shijian == 145800 or shijian == 225800:
            cl.sjb['收盘价'] = zuixinjia
            if ccss > 0:
                for i in cl.sjb['开仓id']:
                    zhanghu.cd_orders(i)  # 撤上次开仓挂单
                cl.sjb['开仓id'] = []
                for i in cl.sjb['平仓id']:
                    zhanghu.cd_orders(i)  # 撤上次开仓挂单
                cl.sjb['平仓id'] = []
                time.sleep(1)
                # 清仓
                cl.jiaoyishezhi[8][3] = zuixinjia+n1
                cl.sjb['开仓id'].append(zhanghu.pc_orders(cl.jiaoyishezhi, -1, ccss))  # 平空
                cl.sjb['收盘状态'] = 1
                time.sleep(0.5)
                print(heyue, time.strftime("%Y-%m-%d %H:%M:%S"), " 收盘工作,记录收盘价撤单等")
            else:
                cl.sjb['开仓id'] = []
                cl.sjb['开仓jg'] = []
                cl.sjb['平仓id'] = []
                cl.sjb['平仓jg'] = []
                cl.sjb['补单id'] = '8'
                shujuku.set('celue.' + cl.name, str(cl.sjb))

    return cl.sjb['挂单状态'], zuixinjia,  cl.sjb['挂开仓价格'], cl.sjb['挂平仓价格']